package com.xiaorun.portal.service.impl;

import com.xiaorun.portal.spider.pipeline.EnterprisePipeline;
import com.xiaorun.portal.spider.pipeline.NewsPipeline;
import com.xiaorun.portal.spider.pipeline.NoticePipeline;
import com.xiaorun.portal.spider.processer.EnterprisePageProcesser;
import com.xiaorun.portal.spider.processer.NewsPageProcesser;
import com.xiaorun.portal.spider.processer.NoticePageProcesser;
import com.xiaorun.portal.spider.processer.PolicyPageProcesser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@Slf4j
public class QuartzService {

    @Autowired
    private EnterprisePipeline enterprisePipeline;

    @Autowired
    private NewsPipeline newsPipeline;

    @Autowired
    private NoticePipeline noticePipeline;

    /*每分钟启动*/
//    @Scheduled(cron = "0 0/1 * * * ?")
//    public void spiderEnterprise() {
//        log.info("start time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//        Spider.create(new EnterprisePageProcesser()).addUrl("http://gdsmp.gdyz.org.cn/sitecn/aspx/productquery.aspx?keyvalue=2014&typename=year").addPipeline(enterprisePipeline).setExitWhenComplete(true).run();
//        Spider.create(new EnterprisePageProcesser()).addUrl("http://gdsmp.gdyz.org.cn/sitecn/aspx/productquery.aspx?keyvalue=2015&typename=year").addPipeline(enterprisePipeline).setExitWhenComplete(true).run();
//        Spider.create(new EnterprisePageProcesser()).addUrl("http://gdsmp.gdyz.org.cn/sitecn/aspx/productquery.aspx?keyvalue=2016&typename=year").addPipeline(enterprisePipeline).setExitWhenComplete(true).run();
//        Spider.create(new EnterprisePageProcesser()).addUrl("http://gdsmp.gdyz.org.cn/sitecn/aspx/productquery.aspx?keyvalue=2017&typename=year").addPipeline(enterprisePipeline).setExitWhenComplete(true).run();
//        Spider.create(new EnterprisePageProcesser()).addUrl("http://gdsmp.gdyz.org.cn/sitecn/aspx/productquery.aspx?keyvalue=2018&typename=year").addPipeline(enterprisePipeline).setExitWhenComplete(true).run();
//        log.info("end time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//    }

    /*每分钟启动*/
//    @Scheduled(cron = "0 0/1 * * * ?")
    @Scheduled(cron = "0 * */1 * * ?")
    public void spiderNews() {
        log.info("spiderNews start time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        Spider spider = Spider.create(new NewsPageProcesser());

//        String[] urls = new String[168];
//        for (int i = 0; i < 168; i++) {
//            urls[i] = "http://sz.shenkexin.com/project/notices.html?idx=" + (i + 1);
//            System.out.println(urls[i]);
//        }

        String[] urls = new String[1];
        urls[0] = "http://sz.shenkexin.com/project/notices.html?idx=1";

        spider.addUrl(urls).addPipeline(newsPipeline).setExitWhenComplete(true).run();
        log.info("spiderNews end time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

    /*爬取公告-每小时*/
    @Scheduled(cron = "0 * */1 * * ?")
    public void spiderNotice() {
        log.info("spiderNotice start time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        Spider spider = Spider.create(new NoticePageProcesser());

//        String[] urls = new String[87];
//        for (int i = 0; i < 87; i++) {
//            urls[i] = "http://www.sz68.com/b/tdgg?s=" + i;
//        }

        String[] urls = new String[1];
        urls[0] = "http://www.sz68.com/b/tdgg?s=0";

        spider.addUrl(urls).addPipeline(noticePipeline).setExitWhenComplete(true).run();
        log.info("spiderNotice end time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

    /*爬取公告-每小时*/
    @Scheduled(cron = "0 * */1 * * ?")
    public void spiderPolicy() {
        log.info("spiderPolicy start time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        Spider spider = Spider.create(new PolicyPageProcesser());

//        String[] urls = new String[70];
//        for (int i = 0; i < 70; i++) {
//            urls[i] = "http://www.sz68.com/b/zcfg?s=" + i;
//        }

        String[] urls = new String[1];
        urls[0] = "http://www.sz68.com/b/zcfg?s=0";

        spider.addUrl(urls).addPipeline(noticePipeline).setExitWhenComplete(true).run();
        log.info("spiderPolicy end time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }


}
